//
//  UIViewController+NEDNaivigationItem.m
//  MoreCoin
//
//  Created by 林宁宁 on 2019/9/19.
//  Copyright © 2019 MoreCoin. All rights reserved.
//

#import "UIViewController+NEDNaivigationItem.h"
#import "UIButton+NED.h"
#import <objc/runtime.h>
#import "UIFont+NED.h"
#import "UIColor+NEDStyle.h"
#import <NEDConstant/NEDConstant.h>
#import <NEDConstant/NEDDefines.h>
#import <NEDCategory/UIButton+NEDWebImage.h>

@implementation UIViewController (NEDNaivigationItem)


- (UIButton *)ned_rightNavcButton{
    return objc_getAssociatedObject(self, @selector(ned_rightNavcButton));
}

- (void)setNed_rightNavcButton:(UIButton *)ned_rightNavcButton{
    objc_setAssociatedObject(self, @selector(ned_rightNavcButton), ned_rightNavcButton, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIButton *)ned_leftNavcButton{
    return objc_getAssociatedObject(self, @selector(ned_leftNavcButton));
}

- (void)setNed_leftNavcButton:(UIButton *)ned_leftNavcButton{
    objc_setAssociatedObject(self, @selector(ned_leftNavcButton), ned_leftNavcButton, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(void)ned_setNavLeftItems:(NSArray *)leftItems{
    [self ned_setNavLeftItems:leftItems handle:nil];
}

-(void)ned_setNavLeftItems:(NSArray *)leftItems
                    handle:(void (^)(UIButton * _Nonnull, NSInteger))leftHandleBlock{
    [self ned_setNavLeftItems:leftItems handle:leftHandleBlock btnConfig:nil];
}

-(void)ned_setNavLeftItems:(NSArray *)leftItems
                    handle:(void (^)(UIButton * _Nonnull, NSInteger))leftHandleBlock
                 btnConfig:(void (^)(UIButton * _Nonnull, NSInteger))configBlock{
    
    self.navigationItem.leftBarButtonItems = @[];
    
    if (!leftItems.count) return;
    
    NSMutableArray *rightButtons = [[NSMutableArray alloc]init];
    for (NSInteger i=0; i<leftItems.count; i++) {
        id item = leftItems[i];
        
        if([item isKindOfClass:[UIView class]]){
            UIBarButtonItem *rightBtnItem = [[UIBarButtonItem alloc] initWithCustomView:(UIView *)item];
            [rightButtons addObject:rightBtnItem];
            continue;
        }
        
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.ned_leftNavcButton = btn;
//        btn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 10);
        btn.ned_touchAreaInsets = UIEdgeInsetsMake(10, 10, 10, 10);
        btn.titleLabel.font = [UIFont ned_pfOfSize:15];
        [btn setTitleColor:[UIColor ned_navigationButtonTitle] forState:UIControlStateNormal];
        btn.tag = i;
        
        btn.frame = CGRectMake(0, 0, Adapted(40), Adapted(40));
        if([item isKindOfClass:[UIImage class]]){
            [btn setImage:item forState:UIControlStateNormal];
        }else if ([item isKindOfClass:[NSString class]]){
            NSURL * url = [NSURL URLWithString:[item stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLQueryAllowedCharacterSet]];
            if (url && [url pathComponents].count > 1){
                [btn ned_setImage:item];
                btn.imageView.contentMode = UIViewContentModeScaleAspectFill;
            }else{
                [btn setTitle:item forState:UIControlStateNormal];
                CGSize sizeFit = [btn sizeThatFits:CGSizeMake(300, Adapted(40))];
                btn.frame = CGRectMake(0, 0, sizeFit.width+10,  Adapted(40));
            }
        }
        __weak typeof(&*btn)weakBtn = btn;
        
        [btn addActionHandler:^(UIButton * sender, NSInteger tag) {
            if (leftHandleBlock) {
                leftHandleBlock(weakBtn,tag);
            }
        }];
        if(configBlock){
            configBlock(btn,i);
        }
//        btn.backgroundColor = [UIColor ned_randomColor];
        
        UIView * itemView = [UIView new];
        itemView.frame = btn.bounds;
        [itemView addSubview:btn];
        
        UIBarButtonItem *rightBtnItem = [[UIBarButtonItem alloc] initWithCustomView:itemView];
        [rightButtons addObject:rightBtnItem];
    }
    rightButtons = (NSMutableArray *)[[rightButtons reverseObjectEnumerator] allObjects];
    self.navigationItem.leftBarButtonItems = rightButtons.copy;
}





-(void)ned_setNavRightItems:(NSArray *)rightItems{
    [self ned_setNavRightItems:rightItems handle:nil];
}

-(void)ned_setNavRightItems:(NSArray *)rightItems
                     handle:(void (^ _Nullable)(UIButton * btn, NSInteger index))rightHandleBlock{
    [self ned_setNavRightItems:rightItems handle:rightHandleBlock btnConfig:nil];
}

-(void)ned_setNavRightItems:(NSArray *)rightItems
                     handle:(void (^)(UIButton * _Nonnull, NSInteger))rightHandleBlock
                  btnConfig:(void (^)(UIButton * _Nonnull, NSInteger))configBlock{
    
    if (!rightItems.count) return;
    
    NSMutableArray *rightButtons = [[NSMutableArray alloc]init];
    for (NSInteger i=0; i<rightItems.count; i++) {
        id item = rightItems[i];
        
        if(rightButtons.count > 0){
            UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
            spaceItem.width = -5;
            [rightButtons addObject:spaceItem];
        }
        
        if([item isKindOfClass:[UIView class]]){
            UIBarButtonItem *rightBtnItem = [[UIBarButtonItem alloc] initWithCustomView:(UIView *)item];
            [rightButtons addObject:rightBtnItem];
            continue;
        }
        
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.ned_rightNavcButton = btn;
//        btn.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, -10);
        btn.ned_touchAreaInsets = UIEdgeInsetsMake(10, 10, 10, 10);
        btn.titleLabel.font = [UIFont ned_pfOfSize:15];
        [btn setTitleColor:[UIColor ned_navigationButtonTitle] forState:UIControlStateNormal];
        btn.tag = i;
        
        btn.frame = CGRectMake(0, 0, Adapted(40), Adapted(40));
        if([item isKindOfClass:[UIImage class]]){
            [btn setImage:item forState:UIControlStateNormal];
        }else if ([item isKindOfClass:[NSString class]]){
            NSURL * url = [NSURL URLWithString:[item stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLQueryAllowedCharacterSet]];
            if (url && [url pathComponents].count > 1){
                [btn ned_setImage:item];
                btn.imageView.contentMode = UIViewContentModeScaleAspectFill;
            }else{
                [btn setTitle:item forState:UIControlStateNormal];
                CGSize sizeFit = [btn sizeThatFits:CGSizeMake(300, Adapted(40))];
                btn.frame = CGRectMake(0, 0, sizeFit.width+10,  Adapted(40));
            }
        }
        __weak typeof(&*btn)weakBtn = btn;
        [btn addActionHandler:^(UIButton * sender, NSInteger tag) {
            if (rightHandleBlock) {
                rightHandleBlock(weakBtn,tag);
            }
        }];
        if(configBlock){
            configBlock(btn,i);
        }

        /// 修改视图可能被系统自动拉伸变大问题
        UIView * itemView = [UIView new];
        itemView.frame = btn.bounds;
        [itemView addSubview:btn];
        
        UIBarButtonItem *rightBtnItem = [[UIBarButtonItem alloc] initWithCustomView:itemView];
        [rightButtons addObject:rightBtnItem];
    }
    rightButtons = (NSMutableArray *)[[rightButtons reverseObjectEnumerator] allObjects];
    self.navigationItem.rightBarButtonItems = rightButtons.copy;
}


@end
